package ch.abcm.remsmobile.REMSMobile.network.networkutils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import ch.abcm.remsmobile.REMSMobile.network.NetworkController;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;

public class NetworkManager {

    private static final String TAG = "NetworkManager";
    private WifiManager wifiManager;

    public NetworkManager(Context context) {
        this.wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    }

    public void createWiFiNetwork() {
        createWiFiNetwork(NetworkController.REMS_SSID, NetworkController.REMS_WIFI_PASSWORD);
    }
    
    public void createWiFiNetwork(String ssid, String password) {
        Log.d(TAG, "Create AccessPoint");
        if (wifiManager.isWifiEnabled()) {
            Log.d(TAG, "Disable Wifi");
            wifiManager.setWifiEnabled(false);
        }
        Method[] wmMethods = wifiManager.getClass().getDeclaredMethods();
        boolean methodFound = false;
        for (Method method : wmMethods) {
            if (method.getName().equals("setWifiApEnabled")) {
                methodFound = true;
                WifiConfiguration netConfig = new WifiConfiguration();
                netConfig.SSID = ssid;
                netConfig.hiddenSSID = false;
                netConfig.status = WifiConfiguration.Status.ENABLED;
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);                
                netConfig.preSharedKey = password;
                
                try {
                    boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig, true);
                    if (apstatus) {
                        Log.d(TAG, "Access Point created");
                    } else {
                        Log.d(TAG, "Access Point creation failed");
                    }

                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        if (!methodFound) {
            Log.d("Splash Activity", "cannot configure an access point");
        }
    }

    public void connectToNetwork(String ssid, String password) {
        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration i : list) {
            if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
                Log.d(TAG, "Found Network networkId: " + i.networkId);
                wifiManager.disconnect();
                Log.d(TAG, "Reconnect to network: " + ssid);
                wifiManager.enableNetwork(i.networkId, true);
                wifiManager.reconnect();
                break;
            }
        }
    }
}
